package com.example.demo.service.impl;



import com.example.demo.entity.Resource;
import com.example.demo.entity.Resource_apply;
import com.example.demo.entity.Resourcetype;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.ResourceRepository;
import com.example.demo.repository.Resource_applyRepository;
import com.example.demo.repository.ResourcetypeRepository;
import com.example.demo.service.ResourceService;
import com.example.demo.vo.ResourceVo;
import com.example.demo.vo.Resource_applyVo;
import com.example.demo.vo.ResourcetypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceRepository resourceRepository;

    @Autowired
    Resource_applyRepository resourceApplyRepository;

    @Autowired
    ResourcetypeRepository resourcetypeRepository;

    //所有资源类型
    @Override
    public PlannerResult<List<Resourcetype>> all_resource_type() {
        return PlannerResult.success(resourcetypeRepository.findAll());
    }

    //添加资源类型
    @Override
    public PlannerResult<String> add_resource_type(ResourcetypeVo resourcetypeVo) {
        Resourcetype resourcetype = new Resourcetype();
        resourcetype.setType(resourcetypeVo.getType());
        return PlannerResult.success("资源类型添加成功");
    }

    //添加资源
    @Override
    public PlannerResult<String> add_resource(ResourceVo resourceVo) {
        Resource resource = new Resource();
        if(resourceVo.getName().equals("其他"))
        {
            resource.setName(resourceVo.getNewname());
            resource.setCurrent_num(resourceVo.getCurrent_num());
            resource.setTotal_num(resourceVo.getTotal_num());
            Resource x = resourceRepository.save(resource);
            if(x.getId()!=null) return PlannerResult.success("资源添加成功");
            else return PlannerResult.error("添加失败");
        }
        else{
            Resource flag = resourceRepository.findByName(resourceVo.getName());
            resource.setName(resourceVo.getName());
            resource.setCurrent_num(flag.getCurrent_num());
            resource.setTotal_num(resourceVo.getTotal_num() + flag.getTotal_num());
            resource.setId(flag.getId());
            Resource x = resourceRepository.save(resource);
            if(x.getId()!=null) return PlannerResult.success("资源添加成功");
            else return PlannerResult.error("添加失败");
        }


    }






    //申请资源
    @Override
    public PlannerResult<Resource> apply_resource(Resource_applyVo resourceApplyVo) {
        Resource_apply resourceapply = new Resource_apply();
        resourceapply.setActivity_id(resourceApplyVo.getActivity_id());
        resourceapply.setResource_id(resourceApplyVo.getResource_id());
        resourceapply.setApplyer_id(resourceApplyVo.getApplyer_id());
        resourceapply.setNum(resourceApplyVo.getNum());
        resourceapply.setState(0);
        resourceapply.setStart_time(resourceApplyVo.getStart_time());
        resourceapply.setEnd_time(resourceApplyVo.getEnd_time());

        Resource_apply result = resourceApplyRepository.save(resourceapply);
        if(result.getId()!=null)   {
            //拿到申请表
            Integer applyid = result.getId();
            Resource_apply resourceApply = resourceApplyRepository.findById(applyid).get();
            Integer resourceid = resourceApplyRepository.findById(applyid).get().getResource_id();
            //拿到资源
            Resource source = resourceRepository.findById(resourceid).get();

            LocalDateTime beginDate2 = resourceApply.getStart_time();
            LocalDateTime endDate2 =resourceApply.getEnd_time();

            //计算已占用量
            Integer sum = 0;

            List<Resource_apply> applys = resourceApplyRepository.findAllByResourceIdandState(resourceid);

            //测试用
            System.out.println(applys);

            for (Resource_apply apply: applys
            ) {
                LocalDateTime beginDate = apply.getStart_time();
                LocalDateTime endDate = apply.getEnd_time();
                if(!(beginDate.compareTo(endDate2) >= 0 || endDate.compareTo(beginDate2) <= 0))
                {
                    sum = sum + apply.getNum();
                }
            }
            if((sum + resourceApply.getNum())>source.getTotal_num())
            {
                source.setCurrent_num(source.getTotal_num()-sum);
                return  PlannerResult.error(source);
            }
            else {
                //通过
                resourceApply.setState(1);
                resourceApplyRepository.save(resourceApply);
                source.setCurrent_num(sum+resourceApply.getNum());
                return PlannerResult.success(source);
            }

        }
        else return PlannerResult.error(null);
    }

    //检测资源
    @Override
    public PlannerResult<Resource> detect_resource(Integer applyid) {
        Resource_apply resourceApply = resourceApplyRepository.findById(applyid).get();
        Integer resourceid = resourceApplyRepository.findById(applyid).get().getResource_id();
        Resource source = resourceRepository.findById(resourceid).get();

        LocalDateTime beginDate2 = resourceApply.getStart_time();
        LocalDateTime endDate2 =resourceApply.getEnd_time();
        Integer sum = 0;

        List<Resource_apply> applys = resourceApplyRepository.findAllByResourceIdandState(resourceid);
        for (Resource_apply apply: applys
             ) {
            LocalDateTime beginDate = apply.getStart_time();
            LocalDateTime endDate = apply.getEnd_time();
            if(!(beginDate.compareTo(endDate2) >= 0 || endDate.compareTo(beginDate2) <= 0))
            {
                sum = sum + apply.getNum();
            }
        }
        if((sum + resourceApply.getNum())>source.getTotal_num())
        {
            source.setCurrent_num(sum);
           return  PlannerResult.error(source);
        }
        else {
            //通过
            resourceApply.setState(1);
            resourceApplyRepository.save(resourceApply);
            source.setCurrent_num(sum+resourceApply.getNum());
            return PlannerResult.success(source);
        }
    }
}
